package com.brainshare.an.service.impl;

import java.util.List;
import com.brainshare.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.brainshare.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.brainshare.an.domain.Exercise;
import com.brainshare.an.mapper.ExerciseProductMapper;
import com.brainshare.an.domain.ExerciseProduct;
import com.brainshare.an.service.IExerciseProductService;

/**
 * 训练产品Service业务层处理
 * 
 * @author wolong
 * @date 2023-03-10
 */
@Service
public class ExerciseProductServiceImpl implements IExerciseProductService 
{
    @Autowired
    private ExerciseProductMapper exerciseProductMapper;

    /**
     * 查询训练产品
     * 
     * @param exerciseProductId 训练产品主键
     * @return 训练产品
     */
    @Override
    public ExerciseProduct selectExerciseProductByExerciseProductId(Long exerciseProductId)
    {
        return exerciseProductMapper.selectExerciseProductByExerciseProductId(exerciseProductId);
    }

    /**
     * 查询训练产品列表
     * 
     * @param exerciseProduct 训练产品
     * @return 训练产品
     */
    @Override
    public List<ExerciseProduct> selectExerciseProductList(ExerciseProduct exerciseProduct)
    {
        return exerciseProductMapper.selectExerciseProductList(exerciseProduct);
    }

    /**
     * 新增训练产品
     * 
     * @param exerciseProduct 训练产品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertExerciseProduct(ExerciseProduct exerciseProduct)
    {
        exerciseProduct.setCreateTime(DateUtils.getNowDate());
        int rows = exerciseProductMapper.insertExerciseProduct(exerciseProduct);
        insertExercise(exerciseProduct);
        return rows;
    }

    /**
     * 修改训练产品
     * 
     * @param exerciseProduct 训练产品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateExerciseProduct(ExerciseProduct exerciseProduct)
    {
        exerciseProduct.setUpdateTime(DateUtils.getNowDate());
        exerciseProductMapper.deleteExerciseByExerciseProductId(exerciseProduct.getExerciseProductId());
        insertExercise(exerciseProduct);
        return exerciseProductMapper.updateExerciseProduct(exerciseProduct);
    }

    /**
     * 批量删除训练产品
     * 
     * @param exerciseProductIds 需要删除的训练产品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExerciseProductByExerciseProductIds(Long[] exerciseProductIds)
    {
        exerciseProductMapper.deleteExerciseByExerciseProductIds(exerciseProductIds);
        return exerciseProductMapper.deleteExerciseProductByExerciseProductIds(exerciseProductIds);
    }

    /**
     * 删除训练产品信息
     * 
     * @param exerciseProductId 训练产品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExerciseProductByExerciseProductId(Long exerciseProductId)
    {
        exerciseProductMapper.deleteExerciseByExerciseProductId(exerciseProductId);
        return exerciseProductMapper.deleteExerciseProductByExerciseProductId(exerciseProductId);
    }

    /**
     * 新增训练产品项目信息
     * 
     * @param exerciseProduct 训练产品对象
     */
    public void insertExercise(ExerciseProduct exerciseProduct)
    {
        List<Exercise> exerciseList = exerciseProduct.getExerciseList();
        Long exerciseProductId = exerciseProduct.getExerciseProductId();
        if (StringUtils.isNotNull(exerciseList))
        {
            List<Exercise> list = new ArrayList<Exercise>();
            for (Exercise exercise : exerciseList)
            {
                exercise.setExerciseProductId(exerciseProductId);
                list.add(exercise);
            }
            if (list.size() > 0)
            {
                exerciseProductMapper.batchExercise(list);
            }
        }
    }
}
